﻿using System;
using Loki.Resources;

namespace Loki.Utils
{
    public class TimeMetric : LokiObject, IDisposable
    {
        private DateTime _Start;

        private string _Message;

        public TimeMetric(string P_Message)
            : this(P_Message, false)
        {
        }

        public TimeMetric(string P_Message, bool P_LogStart)
        {
            _Message = P_Message;
            _Start = DateTime.Now;
            if (P_LogStart)
            {
                Log.DebugFormat(LogStrings.Utils_Timemetric_Start, P_Message, _Start);
            }
        }

        /// <summary>
        /// Releases all resources used by an instance of the <see cref="TimeMetric" /> class.
        /// </summary>
        /// <remarks>
        /// This method calls the virtual <see cref="Dispose(bool)" /> method, passing in <strong>true</strong>, and then suppresses 
        /// finalization of the instance.
        /// </remarks>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        ~TimeMetric()
        {
            Dispose(false);
        }

        /// <summary>
        /// Releases the unmanaged resources used by an instance of the <see cref="TimeMetric" /> class and optionally releases the managed resources.
        /// </summary>
        /// <param name="P_Disposing">Set to <strong>true</strong> to release both managed and unmanaged resources; <strong>false</strong> to release only unmanaged resources.</param>
        protected virtual void Dispose(bool P_Disposing)
        {
            if (P_Disposing)
            {
                Log.DebugFormat(LogStrings.Utils_Timemetric_End, _Message, DateTime.Now - _Start);
            }
        }
    }
}
